package com.charm.restaurant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charm.restaurant.common.Result;
import com.charm.restaurant.entity.Inventory;
import com.charm.restaurant.service.InventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存控制器
 */
@Slf4j
@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    /**
     * 新增库存
     */
    @PostMapping
    public Result<Inventory> save(@RequestBody Inventory inventory) {
        log.info("新增库存：{}", inventory);
        return inventoryService.saveInventory(inventory);
    }

    /**
     * 分页查询库存
     */
    @GetMapping("/page")
    public Result<Page<Inventory>> page(int page, int pageSize, String name, String type) {
        log.info("分页查询库存，page={}, pageSize={}, name={}, type={}", page, pageSize, name, type);
        return inventoryService.pageInventory(page, pageSize, name, type);
    }

    /**
     * 根据ID查询库存
     */
    @GetMapping("/{id}")
    public Result<Inventory> getById(@PathVariable Long id) {
        log.info("根据ID查询库存：{}", id);
        return inventoryService.getInventoryById(id);
    }

    /**
     * 修改库存
     */
    @PutMapping
    public Result<Inventory> update(@RequestBody Inventory inventory) {
        log.info("修改库存：{}", inventory);
        return inventoryService.updateInventory(inventory);
    }

    /**
     * 删除库存
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam String ids) {
        log.info("删除库存：{}", ids);
        // 将字符串id转换为List<Long>
        List<Long> idList = java.util.Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(java.util.stream.Collectors.toList());
        return inventoryService.deleteInventory(idList);
    }

    /**
     * 更新库存数量
     */
    @PutMapping("/stock")
    public Result<String> updateStock(Long id, Integer quantity, String type) {
        log.info("更新库存数量，id={}, quantity={}, type={}", id, quantity, type);
        return inventoryService.updateStock(id, quantity, type);
    }

    /**
     * 查询库存警告
     */
    @GetMapping("/warning")
    public Result<List<Inventory>> warning() {
        log.info("查询库存警告");
        return inventoryService.getInventoryWarning();
    }
}